package com.example.g2daylx2.fg.one

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.example.g2daylx2.R
import com.example.g2daylx2.databinding.ItemParkBinding

import com.example.lib_base.net.entity.CarEntityResultItem

class Parkadapter :PagingDataAdapter<CarEntityResultItem,Parkadapter.ParkViewHodel>(differ) {


    companion object {
        val differ = object : ItemCallback<CarEntityResultItem>() {
            override fun areItemsTheSame(
                oldItem: CarEntityResultItem,
                newItem: CarEntityResultItem
            ): Boolean {
                return oldItem == newItem
            }

            override fun areContentsTheSame(
                oldItem: CarEntityResultItem,
                newItem: CarEntityResultItem
            ): Boolean {
                return oldItem.spaceStatus == newItem.spaceStatus
            }
        }
    }


    class ParkViewHodel(itemView: View) : ViewHolder(itemView) {
        val binging = DataBindingUtil.bind<ItemParkBinding>(itemView)
    }

    override fun onBindViewHolder(holder: ParkViewHodel, position: Int) {
        val bean = getItem(position)!!
        holder.binging?.apply {
            parkNum.text = bean.spaceNumber
            parkStatus.text = "车位类型" + bean.spaceStatus
            parkType.text = "使用人" + bean.userName
            parkCarNo.text = "车牌号" + bean.licensePlate
            parkStatus.setText(bean.spaceStatus)

            if (bean.spaceStatus == "空闲") {
                parkStatus.setTextColor(holder.itemView.context.getColor(R.color.purple_500))
                parkStatus.setBackgroundResource(R.color.purple_500)
            } else if (bean.spaceStatus == "已占用") {
                parkStatus.setTextColor(holder.itemView.context.getColor(R.color.teal_700))
                parkStatus.setBackgroundResource(R.color.teal_700)
            } else {
                parkStatus.setTextColor(holder.itemView.context.getColor(R.color.purple_200))
                parkStatus.setBackgroundResource(R.color.purple_200)
            }
        }



    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ParkViewHodel {
        val view=LayoutInflater.from(parent.context).inflate(R.layout.item_park,parent,false)
        return ParkViewHodel(view)
    }
}
